package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.component.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/product")
public class FileUploadController {

    @Autowired
    MinioService minioService;

    /**
     * 文件上传
     *
     * @return
     */
    @PostMapping("/fileUpload")
    public Result upload(@RequestPart("file") MultipartFile file){

        //上传给minio,返回一个返回地址
        String path = null;
        try {
            path = minioService.uploadFile(file);
            return Result.ok(path);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }

    }
}
